package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JingfeiDept;
import com.ruoyi.system.service.IJingfeiDeptService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 经费管理Controller
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@Controller
@RequestMapping("/system/jingfeidept")
public class JingfeiDeptController extends BaseController
{
    private String prefix = "system/jingfeidept";

    @Autowired
    private IJingfeiDeptService jingfeiDeptService;

    @RequiresPermissions("system:dept:view")
    @GetMapping()
    public String dept()
    {
        return prefix + "/dept";
    }

    /**
     * 查询经费管理列表
     */
    @RequiresPermissions("system:dept:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiDept jingfeiDept)
    {
        startPage();
        List<JingfeiDept> list = jingfeiDeptService.selectJingfeiDeptList(jingfeiDept);
        return getDataTable(list);
    }

    /**
     * 导出经费管理列表
     */
    @RequiresPermissions("system:dept:export")
    @Log(title = "经费管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiDept jingfeiDept)
    {
        List<JingfeiDept> list = jingfeiDeptService.selectJingfeiDeptList(jingfeiDept);
        ExcelUtil<JingfeiDept> util = new ExcelUtil<JingfeiDept>(JingfeiDept.class);
        return util.exportExcel(list, "经费管理数据");
    }

    /**
     * 新增经费管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存经费管理
     */
    @RequiresPermissions("system:dept:add")
    @Log(title = "经费管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiDept jingfeiDept)
    {
        return toAjax(jingfeiDeptService.insertJingfeiDept(jingfeiDept));
    }

    /**
     * 修改经费管理
     */
    @RequiresPermissions("system:dept:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JingfeiDept jingfeiDept = jingfeiDeptService.selectJingfeiDeptById(id);
        mmap.put("jingfeiDept", jingfeiDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存经费管理
     */
    @RequiresPermissions("system:dept:edit")
    @Log(title = "经费管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiDept jingfeiDept)
    {
        return toAjax(jingfeiDeptService.updateJingfeiDept(jingfeiDept));
    }

    /**
     * 删除经费管理
     */
    @RequiresPermissions("system:dept:remove")
    @Log(title = "经费管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiDeptService.deleteJingfeiDeptByIds(ids));
    }

    /**
     * 确认收款
     * @param jingfeipiciId 经费批次ID
     * @param deptid 单位ID
     * @return
     */
//    @RequiresPermissions("system:jingfeidept:edit")
    @Log(title = "单位经费", businessType = BusinessType.UPDATE)
    @PostMapping("/querenshoukuan")
    @ResponseBody
    public JsonResult shoukuan(String jingfeipiciId, String deptid) {
        int result = jingfeiDeptService.updateIsshoudaoByJingfeipiciidAndDeptid(jingfeipiciId, deptid);
        if (result == 1)
            return JsonResult.getSuccessResult("收款成功");
        else if (result == 2)
            return JsonResult.getErrorResult("请等待上级单位发放经费");
        else
            return JsonResult.getErrorResult("收款失败");
    }

    @PostMapping("/fafanglist")
    @ResponseBody
    public JsonResult fafangList(String jingfeipiciId, String deptId) {
        Map<String, List<Map<String, String>>> deptList = jingfeiDeptService.selectFafangList(jingfeipiciId, deptId);
        return JsonResult.getSuccessResult(deptList);
    }

    /**
     * 发放经费
     * @param jingfeipiciId 经费批次ID
     * @param deptIdString 单位ID
     * @return
     */
    // @RequiresPermissions("system:jingfeidept:edit")
    @Log(title = "单位经费", businessType = BusinessType.UPDATE)
    @PostMapping("/fafang")
    @ResponseBody
    public JsonResult fafang(String jingfeipiciId, String deptIdString, String zhuanjiaIdString) {

        String[] deptIds = deptIdString.split(",");
        String[] zhuanjiaIds = zhuanjiaIdString.split(",");

        int result = jingfeiDeptService.updateIsfafangByJingfeipiciidAndDeptid(jingfeipiciId, deptIds, zhuanjiaIds);

        if (result == 1)
            return JsonResult.getSuccessResult("经费发放成功");
        else if (result == 0)
            return JsonResult.getErrorResult("至少选择1个单位发放");
        else
            return JsonResult.getErrorResult("经费发放失败");
    }
}
